package com.news.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.news.common.lang.Const;
import com.news.common.lang.Result;
import com.news.entity.SysUser;
import com.news.service.SysUserService;
import com.news.utils.JWTUtils;
import com.news.vo.LoginVo;
import com.news.vo.PasswordVo;
import com.news.vo.RegisterVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

//事务回滚注解
@Api("用户登录，登出接口")
@Transactional
@RestController
public class UserController {
    @Autowired
    SysUserService userService;

    @GetMapping("/user/update/{uid}")
    public Result userId(@PathVariable("uid") Long uid){
        return Result.succ(userService.getById(uid));
    }

    @PostMapping("/login")
    public Result login(@Validated @RequestBody LoginVo loginVo, HttpServletResponse respone){
        //断言判断用户
        SysUser user = userService.getOne(new QueryWrapper<SysUser>().eq("username",loginVo.getUsername()));
        Assert.notNull(user,"用户不存在");

        if(!user.getPassword().equals(SecureUtil.md5(loginVo.getPassword()+ Const.SALT))){
            return Result.fail("密码不正确");
        }
        String jwt = JWTUtils.createToken(user.getUid());
        respone.setHeader("Authorization",jwt);
        respone.setHeader("Access-control-Expose-Headers","Authorization");

        //返回前端保存，减少访问后端
        return Result.succ(MapUtil.builder()
                .put("id",user.getUid())
                .put("nickname",user.getNickname())
                .put("username",user.getUsername())
                .put("avatar",user.getAvatar())
                .put("phone",user.getPhone())
                .put("email",user.getEmail())
                .put("createTime",user.getCreateTime())
                .map()
        );
    }

    //修改密码接口
    @PostMapping("/user/updatePass")
    public Result updateUserPass(@Validated @RequestBody PasswordVo passwordVo){
        SysUser user = userService
                .getOne(new QueryWrapper<SysUser>()
                        .eq("password",SecureUtil.md5(passwordVo.getCurrentPass()+Const.SALT)));
        Assert.notNull(user,"旧密码不正确");
        user.setPassword(SecureUtil.md5(passwordVo.getPassword()+Const.SALT));
        user.setUpdateTime(LocalDateTime.now());
        userService.updateById(user);
        return Result.succ("");
    }

    //更新用户接口
    @PostMapping("/user/update")
    public Result updateUser(@Validated @RequestBody SysUser user){
        user.setUpdateTime(LocalDateTime.now());
        userService.updateById(user);
        return Result.succ(user);
    }

}
